﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace CetWin.Phone.Controls
{
    public class TileBoxItem : Grid
    {
        public TileBoxItem(FrameworkElement element1, FrameworkElement element2)
        {

            for (int i = 0; i < 2; i++)
            {
                ColumnDefinition cd = new ColumnDefinition();
                if (i == 0)
                    cd.Width = new GridLength(0, GridUnitType.Auto);
                this.ColumnDefinitions.Add(cd);
            }
            this.Children.Add(element1);
            if (element2 != null)
            {
                Grid.SetColumn(element2, 1);
                this.Children.Add(element2);
            }
        }
    }


    [ContentProperty("Tiles")]
    public class TileBox:Grid
    {
        public TileBox()
        {
            Tiles = new ObservableCollection<TileItem>();
            Tiles.CollectionChanged += new NotifyCollectionChangedEventHandler(Tiles_CollectionChanged);
            for (int i = 0; i < 2; i++)
            {
                ColumnDefinition cd = new ColumnDefinition();
                if (i == 0)
                    cd.Width = new GridLength(0, GridUnitType.Auto);
                this.ColumnDefinitions.Add(cd);
            }

            for (int i = 0; i < 4; i++)
            {
                RowDefinition rd = new RowDefinition();
                rd.Height = new GridLength(0, GridUnitType.Auto);
                this.RowDefinitions.Add(rd);
            }
            this.Loaded += new RoutedEventHandler(TileBox_Loaded);
        }

        void TileBox_Loaded(object sender, RoutedEventArgs e)
        {
           
            UpdateElementPosition();
            this.Loaded -= new RoutedEventHandler(TileBox_Loaded);
        }

        private void UpdateElementPosition()
        {
            for (int i = 0; i < this.Tiles.Count; i += 2)
            {
                //1 0,0 
                //2 1,0 
                //3 0,1
                //4 1,1
                TileBoxItem item = null;
                if ((i + 1) == this.Tiles.Count)
                    item = new TileBoxItem(this.Tiles[i], null);
                else
                    item = new TileBoxItem(this.Tiles[i], this.Tiles[i + 1]);

                Grid.SetRow(item, i / 2);

                Grid.SetColumnSpan(item, 2);
                this.Children.Add(item);
                //var element = this.Children[i] as FrameworkElement;
                ////element.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;


            }
        }

        void Tiles_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            //switch (e.Action)
            //{
            //    case NotifyCollectionChangedAction.Add:
            //        this.Children.Add(e.NewItems[0] as UIElement);
            //        //UpdateElementPosition();
            //        break;
            //    case NotifyCollectionChangedAction.Remove:
            //        this.Children.Remove(e.NewItems[0] as UIElement);
            //        break;
            //    case NotifyCollectionChangedAction.Replace:
            //        break;
            //    case NotifyCollectionChangedAction.Reset:
            //        break;
            //    default:
            //        break;
            //}
        }

        public void Reset(IList<string> list)
        {
            Tiles.Clear();
            this.Children.Clear();
            if (list != null)
                foreach (var item in list)
                {
                    Tiles.Add(new TileItem() { Content = item });
                }
            UpdateElementPosition();
        }

        public ObservableCollection<TileItem> Tiles { get; set; }
        
    }

    public class TileItem:Button
    {
        public TileItem()
        {
            DefaultStyleKey = typeof(TileItem);
        }
    }
}
